package main

import (
	"flag"
	"fmt"

	"lottery-zero/external/database"
	"lottery-zero/external/schedule"
	"lottery-zero/internal/config"
	"lottery-zero/internal/handler"
	"lottery-zero/internal/shared"
	"lottery-zero/internal/svc"

	"github.com/tal-tech/go-zero/core/conf"
	"github.com/tal-tech/go-zero/core/logx"
	"github.com/tal-tech/go-zero/rest"
	"github.com/tal-tech/go-zero/rest/httpx"
)

var configFile = flag.String("f", "etc/lottery-zero-api.yaml", "the config file")

func prepare(c config.Config) {
	// 数据库初始化
	database.NewLuckDBConn(c.Pgdns)
	// 准备好所有的数据在内存
	// ssq.NewDoubleBollAll()
	// 任务调度
	schedule.Schedule()
	schedule.SsqUpdate()
	schedule.LottoUpdate()
}

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	prepare(c)
	logx.MustSetup(logx.LogConf{Mode: "file", KeepDays: 30, Compress: true, Path: "/tmp/logs"})
	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	httpx.SetErrorHandler(shared.ErrorHandler)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
